package com.ruoyi.fin.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.api.event.EventListener;
import com.ruoyi.api.event.EventType;
import com.ruoyi.api.event.OrderExecutionEvent;
import com.ruoyi.fin.domain.FinTransaction;
import com.ruoyi.fin.service.IFinTransactionService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.ShiroUtils;

import javax.annotation.PostConstruct;

/**
 * 销售订单执行事件监听器
 */
@Component
public class SalesOrderExecutionListener implements EventListener<OrderExecutionEvent> {

    private static final Logger logger = LoggerFactory.getLogger(SalesOrderExecutionListener.class);

    @Autowired
    private IFinTransactionService finTransactionService;
    
    @Autowired
    private com.ruoyi.api.event.EventCenter eventCenter;
    
    @PostConstruct
    public void init() {
        // 注册监听器
        eventCenter.registerListener(this);
        logger.info("Registered SalesOrderExecutionListener");
    }

    @Override
    public void onEvent(OrderExecutionEvent event) {
        if (event.getEventType() != EventType.SALES_ORDER_EXECUTED) {
            return;
        }
        
        logger.info("Processing sales order execution event for order ID: {}", event.getOrderId());
        
        // 获取指定的账户ID，如果没有指定则使用默认账户
        Long accountId = 1L; // 默认账户ID
        if (event.getData("accountId") != null) {
            try {
                accountId = Long.valueOf(event.getData("accountId").toString());
                logger.info("Using specified account ID: {}", accountId);
            } catch (Exception e) {
                logger.error("Error parsing account ID, using default account", e);
            }
        }
        
        // 创建收入类型的财务交易记录
        FinTransaction transaction = new FinTransaction();
        transaction.setTransactionNo("ST" + IdUtils.fastSimpleUUID().substring(0, 10));
        transaction.setAccountId(accountId); // 使用指定账户ID
        transaction.setTransactionType("0"); // 0-收入
        transaction.setAmount(event.getTotalAmount()); // 收入为正数
        transaction.setTransactionDate(DateUtils.getNowDate());
        transaction.setCategory("销售收入");
        transaction.setDescription("销售订单:" + event.getOrderCode());
        transaction.setSourceType("sales");
        transaction.setSourceId(event.getOrderId());
        transaction.setSourceCode(event.getOrderCode());
        transaction.setRemark("销售订单收入");
        transaction.setTransactionStatus("0"); // 0-待处理
        transaction.setCreatedBy(ShiroUtils.getUserId());
        
        // 保存交易记录
        finTransactionService.insertFinTransaction(transaction);
    }

    @Override
    public EventType getSupportedEventType() {
        return EventType.SALES_ORDER_EXECUTED;
    }
    
    @Override
    public Class<OrderExecutionEvent> getSupportedEventClass() {
        return OrderExecutionEvent.class;
    }
} 